#include "StdAfx.h"
#include "iPod.h"

namespace iPod
{

#define SONG_LIST_FILE_NAME "input_song.txt"
#define MAX_SIZE_IN_BYTE    ( 25 * 1024 * 1024 ) // 25MB
    
//===========================================================================
void initialize( void )
{
    cleanup();
    
    const unsigned int size = MAX_SIZE_IN_BYTE / sizeof( Song );
    if( !size )
        return;
    iPod::songs = new Song[ size ];

    std::ifstream file( SONG_LIST_FILE_NAME );

    unsigned int index = 0;
    if( !file.bad() )
    {
        std::string line;
        while( std::getline( file, line ) && index < size )
            if( line.length() > 0 )
                if( !iPod::songs[ index ].title.length() )
                    iPod::songs[ index ].title = line;
                else
                if( !iPod::songs[ index ].artist.length() )
                    iPod::songs[ index ].artist = line;
                else
                    iPod::songs[ index++ ].size = std::atoi( line.c_str() );
    }
    iPod::count = index;

    do
    {
        const char * menu = "Welcome to My Ipod!\r\n"
                            "1. Show the playlist\r\n"
                            "2. Remove a song\r\n"
                            "3. Cleanup my ipod\r\n"
                            "4. Shuffle the playlist\r\n"
                            "5. Exit\r\n"
                            "Please choose your options :";
        std::cout << menu;
        std::fflush( stdin );

        int choice = 0;
        std::cin >> choice;

        switch( choice )
        {
            case 1  : iPod::show_playlist(); break;
            case 2  : iPod::remove_a_song(); break;
            case 3  : iPod::cleanup();       break;
            case 4  : iPod::shuffle();       break;
            case 5  : iPod::exit();          return;
            default :                        break;
        }
    } while( true );
}

//===========================================================================
void show_playlist( void )
{
    for( unsigned int i = 0; i < iPod::count; i++ )
        iPod::display( iPod::songs[ i ] );
}

//===========================================================================
void remove_a_song( void )
{
    std::fflush( stdin );
    
    std::string title;
    std::cout << "title : ";
    std::getline( std::cin, title );

    int found = iPod::find_a_song( title.c_str() );
    if( found < 0 )
    {
        std::cout << "cannot find a song with a given title" << std::endl;
        return;
    }

    unsigned int count = iPod::count - 1;
    if( !count )
    {
        iPod::cleanup();
        return;
    }

    Song *       songs = new Song[ count ];
    unsigned int index = 0;
    for( unsigned int i = 0; i < iPod::count; i++ )
        if( i != found )
            songs[ index++ ] = iPod::songs[ i ];

    iPod::cleanup();
    iPod::songs = songs;
    iPod::count = count;
}

//===========================================================================
void cleanup( void )
{
    if( iPod::songs )
    {
        delete [] iPod::songs;
        iPod::songs = NULL;
        iPod::count = 0;
    }
}

//===========================================================================
void shuffle( void )
{
    iPod::display( iPod::songs[ rand() % iPod::count ] );
}

//===========================================================================
void exit( void )
{
    iPod::cleanup();
}

//===========================================================================
int find_a_song( const char * title )
{
    if( !title || !*title )
        return -1;

    for( unsigned int i = 0; i < iPod::count; i++ )
        if( !strcmp( title, iPod::songs[ i ].title.c_str() ) )
            return i;

    return -2;
}

//===========================================================================
void display( Song & song )
{
    std::cout << "================================" << std::endl;
    std::cout << "title  : " << song.title.c_str()  << std::endl;
    std::cout << "artist : " << song.artist.c_str() << std::endl;
    std::cout << "size   : " << song.size           << std::endl;
}

}; // end of namespace iPod